package bug 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	
	import bug.Castle;
	import bug.Scene;
	
	public class bugTest extends Sprite
	{	
		private var _scene:Scene;
		private var _uiCountoler:UIControler;
		private var _controler:Controler;
		private var _ai:Wisdom;
		private var _currentLevel:int=1;//当前关卡
		private static const KEY_PATH:String="mc_path";
		private static const KEY_BG:String="mc_bgscene";
		public function bugTest()
		{
			super();
			mc_path1
			
			mc_path2
			
			mc_path3
			mc_path4
			mc_path5
			mc_path6
			mc_path7
			mc_path8
			mc_path9
			
			mc_path10
			mc_path11
			mc_path12
			mc_path13
			
			mc_bgscene1
			mc_bgscene2
			mc_bgscene3
			mc_bgscene4
			mc_bgscene5
			mc_bgscene6
			mc_bgscene7
			mc_bgscene8
			mc_bgscene9
			mc_bgscene10
			mc_bgscene11
			mc_bgscene12
			mc_bgscene13
		}
		
		public function start(level:int):void{
			_currentLevel=level;
			if(_scene==null){
				_scene=new Scene();
			}
			if(_controler==null){
				_controler = new Controler(_scene);
			}else{
				_controler.reset(_scene);
			}
			if(_uiCountoler==null){
				_uiCountoler = new UIControler(_scene,this);
			}else{
			    _uiCountoler.reset(_scene);
			}
			if(_ai==null){
				_ai=new Wisdom(_scene);
			}else{
			    _ai.reset(_scene);
			}
			_scene.addCastles(initCastle());
			addChild(_scene);	
			play();
		}
		
		public function dispose():void{
		    stop();
			_controler.dispose();
			_uiCountoler.dispose();
			_ai.dispose();
			_scene.dispose();
		}
		
		public function play():void{
			this.addEventListener(Event.ENTER_FRAME,onEnterFrame); 
		}
		
		public function stop():void{
			this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);	
		}
		
		private function onEnterFrame(e:Event):void{
		   var isOver:Boolean = _scene.step();
			_ai.updata();
//			if(isOver){
//			    stop();
//			}
		}
		
		private function initCastle():Vector.<Castle>{
			var levelData:XMLList = Main.staticData.getLevelDataByLevel(_currentLevel);
			var list:Vector.<Castle> = new Vector.<Castle>();
			var c:Class = ApplicationDomain.currentDomain.getDefinition(KEY_PATH+_currentLevel) as Class;
			var path:DisplayObject = new c();
			_scene.setPath(path);
			c =   ApplicationDomain.currentDomain.getDefinition(KEY_BG+_currentLevel) as Class;
			var bg:DisplayObject = new c();
			_scene.setBg(bg);
			
			var it:int;
			for each(var i:XML in  levelData) 
			{
				it++;
				list.push(new Castle(path["spawn"+it],_scene,i.@power,i.@autoGrow=="true",i.@antCount,i.@isNeutral=="true"));
			}
			it=0;
			var strs:Array;
			for each(i in  levelData) 
			{
				strs=String(i.@neighbors).split(",");
				var temp:Array=[];
				for (var j:int = 0; j < strs.length; j++) 
				{
					 temp.push(list[strs[j]]);
				}
				
				list[it].setNext.apply(null,temp);
				it++;
			}
			return list;
		}
	}
}